<?php
namespace app\admin\controller;
use app\BaseController;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Request;
use think\facade\Route;
use think\facade\View;

class Good extends BaseController
{
    public function good()
    {
        return view();
    }
    public function goodAdd()
    {
        return view();
    }
    public function goodList()
    {
        $keywords = Request::post('keywords');
        $acity = Request::param('acity');
        $jia = Request::param('jia');
        $time = Request::param('time');
        $times = Request::param('times');
        $post = Request::post();
        $where = [];
        if (isset($keywords) && $keywords <> '')   $where[] = ['g.name','like',"%{$keywords}%"];
        if (isset($post['is_down']) && count(['is_down'])==1)   $where[] = ['is_down','in',$post['is_down']];
        if (isset($post['is_show']) && $post['is_show']<>'')   $where[] = ['is_show','=',$post['is_show']];
        if (isset($post['is_hot']) && $post['is_hot']<>'')   $where[] = ['is_hot','=',$post['is_hot']];
        if (isset($time) && !empty($time))  $where[] = ['g.addtime','between time',$times.','.$time];
        if (isset($acity) && !empty($acity))  $where[] = ['price','<=',$post['acity']];
        if (isset($jia) && !empty($jia))  $where[] = ['price','>=',$post['jia']];
         if (isset($post['cid']) && !empty($post['cid']))   $where[] = ['cid','=',$post['cid']];
      View::assign($post);
      $list = Db::name('goods')
          ->alias('g')
          ->join('category c','g.cid = c.id')
          ->where($where)
          ->field('g.*')
          ->field('c.name cname')
          ->select();
        $list = Db::name('goods')
            ->alias('g')
            ->join('category c','g.cid = c.id')
            ->where($where)
            ->field('g.*')
            ->field('c.name cname')
            ->paginate([
            'list_rows' =>5,
            'query'   =>  [
                'id'=>1,
                'name'  =>'tang'
            ]

        ]);


    $fen=Db::table("ecm_category")->field("id,name")->where("parentid","0")->select()->toArray();
    foreach ($fen as &$v){
        $v['son']=Db::table("ecm_category")->field("id,name")->where("parentid",$v['id'])->select()->toArray();

    }
       View::assign("fen",$fen);
        View::assign('list',$list);
        return View::fetch();
    }
    public function cate()
    {
        $parentid = Request::get('id',0);
      $list = Db::table('ecm_category')->order('sort')->where('parentid','=',$parentid)->select()->toArray();
      if (Cache::has('all')){
          $all = Cache::get('all');
      }else{
          $all = Db::name('category')->select()->toArray();
          Cache::set('all',$all,10);
      }
      foreach ($list as &$v){
          foreach ($all as $vv){
              if ($v['id'] == $vv['parentid']){
                  $v['son'] =1;
              }
          }
      }
       View::assign('list',$list);
       return View::fetch();
    }
    public function cateedit()
    {
        return view();
    }


    public function pinpai()
    {

        $param = Request::param();
        $whereOr = $where = [];
        if (isset( $param['keywords']) && $param['keywords'] != '')  $where[] = ['brand_name','like','%'.$param['keywords'].'%'];
        if (isset($param['if_show']) && count($param['if_show']) ==1)   $where[] = ['if_show','in',$param['if_show']];
        if (isset($param['recommended']) && $param['recommended'] <> '')   $where[] = ['recommended','=',$param['recommended']];
        if (isset($param['is_img']) && count($param['is_img']) == 1){
            if ($param['is_img'][0] == 1){
                $where[] = ['brand_logo','<>',''];
            }else{
                $whereOr[] = ['brand_logo','=',null];
                $whereOr[] = ['brand_logo','=',''];
            }
        }
       $xian = Db::table('ecm_brand')
           ->where($where)
           ->where(function ($q) use($whereOr){$q->whereOr($whereOr);})
           ->order('sort_order')
           ->select();
        $xian = Db::name('brand')
            ->where($where)
            ->where(function ($q) use($whereOr){$q->whereOr($whereOr);})
            ->order('sort_order')
            ->paginate([
                'list_rows' =>5,
                'query'   =>  [
                    'id'=>1,
                    'name'  =>'tang'
                ]

            ]);
       View::assign('xian',$xian);
       return view('',$param);
    }

}